package com.blog.controller.admin;

import com.blog.constant.Types;
import com.blog.constant.WebConst;
import com.blog.dto.MetaDto;
import com.blog.service.meta.MetaService;
import com.blog.utils.ResultVoUtil;
import com.blog.vo.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 分页和标签管理页
 *
 * @author wmb
 *
 * @since 2021-2-9
 */
@Api("分类和标签")
@Controller
public class CategoryController {

    private static final Logger LOGGER = LogManager.getLogger(CategoryController.class);

    @Autowired
    @Qualifier("metaService")
    private MetaService metaService;


    @ApiOperation("进入分类/标签管理页")
    @GetMapping("/admin/category")
    public String categoryIndex(@ApiIgnore HttpServletRequest request) {
        List<MetaDto> categoryList = metaService.getMetaList(Types.CATEGORY.getType(), null, WebConst.MAX_POSTS);
        List<MetaDto> tagList = metaService.getMetaList(Types.TAG.getType(), null, WebConst.MAX_POSTS);
        request.setAttribute("categories", categoryList);
        request.setAttribute("tags", tagList);
        return "admin/category";
    }

    @ApiOperation("保存标签")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cname", value = "分类名", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "mid", value = "meta编号", paramType = "query", dataType = "int")
    })
    @PostMapping("/admin/category/save")
    @ResponseBody
    public ResultVo saveCategory(@NotBlank(message = "分类名不允许为空") @RequestParam("cname") String name,
                                 @RequestParam(value = "mid", required = false) Integer id) {
        metaService.saveMeta(Types.CATEGORY.getType(), name, id);
        return ResultVoUtil.success();
    }

    @ApiOperation("删除分类")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mid", value = "项目主键id", paramType = "query", dataType = "int", required = true)
    })
    @PostMapping(value = "/admin/category/delete")
    @ResponseBody
    public ResultVo deleteCategory(@NotNull(message = "项目主键不允许为空") @RequestParam("mid") Integer id) {
        metaService.deleteMetaById(id);
        return ResultVoUtil.success();
    }
}
